package leetcode.day_2021_04_12_2021_04_18;

import java.util.ArrayList;
import java.util.List;

/**
 * 89. 格雷编码
 * https://leetcode-cn.com/problems/gray-code/
 * https://leetcode-cn.com/problems/gray-code/solution/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-by--12/
 */
public class LeetCode0089 {
    public static List<Integer> grayCode(int n) {
        List<Integer> list = new ArrayList<>();
        list.add(0);
        for (int i=0; i<n; i++) {
            int num = 1 << i;
            for (int j=list.size()-1; j>=0; j--) {
                list.add(list.get(j)+num);
            }
        }
        return list;
    }

    public static void main(String[] args) {
        System.out.println(grayCode(2).toString());
    }
}
